﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RibbonEditor.CommandUIDefinitionClasses;

namespace RibbonEditor.Controls.CommandUIDefinitions
{
    public partial class TabControl : UserControl, IControlRulable
    {
        private Tab innerTab;

        public Tab InnerTab
        {
            get { return innerTab; }
            set { innerTab = value; }
        }

        public TabControl()
        {
            InitializeComponent();
            this.innerTab = new Tab();
            this.FillControls();
        }

        public TabControl(Tab tab):this()
        {
            this.innerTab = tab;
            this.FillControls();
        }

        private void FillControls()
        {
            try
            {
                this.rcCommand.Value = this.innerTab.Command;
                this.rcCssClass.Value = this.innerTab.CssClass;
                this.rcDescription.Value = this.innerTab.Description;
                this.strId.Value = this.innerTab.Id;
                this.icSequence.Value = this.innerTab.sequence;
                this.rcTitle.Value = this.innerTab.Title;
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Save()
        {
            try
            {
                if (UserControlHelper.IsControlValid(this))
                {
                    this.innerTab.Command = this.rcCommand.Value;
                    this.innerTab.CssClass = this.rcCssClass.Value;
                    this.innerTab.Description = this.rcDescription.Value;
                    this.innerTab.Id = this.strId.Value;
                    this.innerTab.sequence = this.icSequence.Value;
                    this.innerTab.Title = this.rcTitle.Value;

                    UserControlHelper.CleanDirtyFields(this);
                }
                else
                {
                    MessageBox.Show(this, "Please fill all required fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured:" + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void TabControl_Leave(object sender, EventArgs e)
        {
            try
            {
                if (UserControlHelper.LeaveControl(this))
                {
                    this.Save();
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured:" + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IControlRulable Members


        public void BuildObjectXml(System.Xml.XmlNode parentNode)
        {
            try
            {
                this.innerTab.AddNode(parentNode);
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured:" + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
